package com.junchenrobot.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junchenrobot.mes.core.TaskStored;
import com.junchenrobot.mes.dao.WinderTaskDao;
import com.junchenrobot.mes.dao.WinderTimeDao;
import com.junchenrobot.mes.domain.Device;
import com.junchenrobot.mes.domain.WinderTask;
import com.junchenrobot.mes.domain.WinderTaskSpare;
import com.junchenrobot.mes.domain.WinderTime;
import com.junchenrobot.mes.service.TaskService;
import com.junchenrobot.mes.vo.*;
import com.junchenrobot.mes.vo.param.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class TaskServiceImpl implements TaskService {
    
    
    private final WinderTaskDao winderTaskDao;

    private final WinderTimeDao winderTimeDao;

    private final TaskStored stored;
    
    @Autowired
    public TaskServiceImpl(WinderTaskDao winderTaskDao, TaskStored stored,WinderTimeDao winderTimeDao) {
        this.winderTaskDao = winderTaskDao;
        this.stored = stored;
        this.winderTimeDao = winderTimeDao;
    }
    
    
    /**
     * 任务列表
     *
     * @param pageParam 页面参数
     * @return {@link List}<{@link TaskInfoVo}>
     */
    @Override
    public PageRecordVo<TaskInfoVo> listTask(PageParam pageParam) {
        // 开启分页
        PageHelper.startPage(pageParam.getPage(), pageParam.getNum());
        // 查询未完成任务数据
        List<WinderTask> winderTasks = winderTaskDao.listUnFinishedTask();
        
        List<TaskInfoVo> collect = winderTasks.stream().map(v -> {
            TaskInfoVo taskInfoVo = new TaskInfoVo();
            taskInfoVo.setTaskId(v.getId());
            taskInfoVo.setTaskCode(v.getTaskCode());
            taskInfoVo.setTaskWarnTime(v.getTaskWarnTime());
            taskInfoVo.setTaskWinderId(v.getTaskWinderId());
            taskInfoVo.setTaskWinderLsh(v.getTaskWinderLsh());
            taskInfoVo.setTaskWinderLine(v.getTaskWinderLine());
            taskInfoVo.setTaskWinderSpinningCode(v.getTaskWinderSpinningCode());
            taskInfoVo.setTaskPoleCode(v.getTaskPoleCode());
            taskInfoVo.setTaskLocation(v.getTaskLocation());
            taskInfoVo.setTaskDeviceId(v.getTaskDeviceId());
            taskInfoVo.setTaskDeviceName(v.getTaskDeviceName());
            taskInfoVo.setTaskType(v.getTaskType());
            taskInfoVo.setTaskExecuteStatus(v.getTaskExecuteStatus());
            taskInfoVo.setTaskRemark(v.getTaskRemark());
            return taskInfoVo;
        }).collect(Collectors.toList());
        // 分页数据
        PageInfo<TaskInfoVo> pageInfo = new PageInfo<>(collect);
        return new PageRecordVo<>(pageInfo.getPageNum(), pageInfo.getTotal(), collect);
    }

    /**
     * 列出历史记录
     * @param pageParam 页面参数
     * @return
     */
    @Override
    public PageVo historyList(PageParam pageParam) {
        // 开启分页

       Integer page =  (pageParam.getPage() - 1) * pageParam.getNum();
        List<WinderTask> winderTasks = winderTaskDao.ListAll(page, pageParam.getNum());
        List<TaskHistoryVo> collect = winderTasks.stream().map(v -> {
            TaskHistoryVo taskHistoryVo = new TaskHistoryVo();
            taskHistoryVo.setTaskCode(v.getTaskCode());
            taskHistoryVo.setTaskDeviceName(v.getTaskDeviceName());
            taskHistoryVo.setTaskPoleCode(v.getTaskPoleCode());
            taskHistoryVo.setTaskType(v.getTaskType());
            taskHistoryVo.setTaskWinderLine(v.getTaskWinderLine());
            taskHistoryVo.setTaskWinderSpinningCode(v.getTaskWinderSpinningCode());
            taskHistoryVo.setTaskWinderLsh(v.getTaskWinderLsh());
            taskHistoryVo.setTaskLocation(v.getTaskLocation());
            taskHistoryVo.setTaskStartTime(v.getTaskStartTime());
            taskHistoryVo.setTaskFinishedTime(v.getTaskFinishedTime());
            return taskHistoryVo;
        }).collect(Collectors.toList());

        Long count = winderTaskDao.selectCount();
PageVo pageVo = new PageVo();
pageVo.setList(collect);
pageVo.setCount(count);
        return pageVo;
    }

    /**
     * 查询等待分配的任务列表
     *
     * @return {@link List}<{@link WaitingTaskVo}>
     */
    @Override
    public List<WaitingTaskVo> listWaitingTask() {
        List<WinderTaskSpare> winderTaskSpareList = stored.listWinderTask();
        List<WaitingTaskVo> waitingTaskVoList = winderTaskSpareList.stream().filter(Objects::nonNull).map(v -> {
            WaitingTaskVo waitingTaskVo = new WaitingTaskVo();
            waitingTaskVo.setTaskCode(v.getTaskCode());
            waitingTaskVo.setTaskWinderId(v.getTaskWinderId());
            waitingTaskVo.setTaskLocation(v.getTaskLocation());
            waitingTaskVo.setTaskWarnTime(v.getTaskWarnTime());
            waitingTaskVo.setTaskWinderLsh(v.getTaskWinderLsh());
            waitingTaskVo.setTaskWinderLine(v.getTaskWinderLine());
            waitingTaskVo.setTaskWinderSpinningCode(v.getTaskWinderSpinningCode());
            waitingTaskVo.setTaskType(v.getTaskType());
            return waitingTaskVo;
        }).collect(Collectors.toList());
//        for (WaitingTaskVo waitingTaskVo : waitingTaskVoList) {
//            waitingTaskVo
//        }
        return waitingTaskVoList;
    }

    @Override
    public List<WinderTimeVo> listWinderTime() {
        List<WinderTime> winderTimes = winderTimeDao.listWinder();
        return winderTimes.stream().filter(Objects::nonNull).map(v -> {
            WinderTimeVo winderTimeVo = new WinderTimeVo();
            winderTimeVo.setId(v.getId());
            winderTimeVo.setTaskWinderLine(v.getWinderLine());
            winderTimeVo.setBurstTime(v.getBurstTime());
            winderTimeVo.setWinderName(v.getWinderName());
            winderTimeVo.setTaskWinderSpinningCode(v.getWinderSpinningCode());
            winderTimeVo.setModelNumber(v.getModelNumber());
            winderTimeVo.setVariety(v.getVariety());
            return winderTimeVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<WinderTimeVo> blurListWinderTime(String winderName) {
        List<WinderTime> winderTimes = winderTimeDao.blurListWinder(winderName);
        return winderTimes.stream().filter(Objects::nonNull).map(v -> {
            WinderTimeVo winderTimeVo = new WinderTimeVo();
            winderTimeVo.setBurstTime(v.getId());
            winderTimeVo.setTaskWinderLine(v.getWinderLine());
            winderTimeVo.setBurstTime(v.getBurstTime());
            winderTimeVo.setWinderName(v.getWinderName());
            winderTimeVo.setTaskWinderSpinningCode(v.getWinderSpinningCode());
            winderTimeVo.setModelNumber(v.getModelNumber());
            winderTimeVo.setVariety(v.getVariety());
            return winderTimeVo;
        }).collect(Collectors.toList());
    }

    /**
     * 查询avg小车列表
     * @return  avg小车列表
     */
    @Override
    public List<DeviceVo> deviceVoList() {
        List<Device> deviceList = stored.deviceList();
        return deviceList.stream().filter(Objects::nonNull).map(v -> {
            DeviceVo deviceVo = new DeviceVo();
            deviceVo.setDeviceName(v.getDeviceName());
            deviceVo.setDeviceStatus(v.getDeviceStatus());
            deviceVo.setDeviceBattery(v.getDeviceBattery());
            deviceVo.setDeviceLeftPole(v.getDeviceLeftPole());
            deviceVo.setDeviceRightPole(v.getDeviceRightPole());
            return deviceVo;
        }).collect(Collectors.toList());
    }

}
